#coding=utf-8
import os
import sys

import common_util
from common_util import find_first_CHINESE_CHAR
    
class WordAttributes():
    def __init__(self, line):
        line = line.strip()
        self.full = line
        self.types = []
        self.honetic_symbols = ""
    def parse(self):
        self.__parse_type(0)
    def __parse_type(self, index):
        content = self.full[index:]
        pos = content.find(".")
        if pos < 0:
            return
        pos_before = content.rfind(' ', 0, pos)
        if pos_before < 0:
            return
        #if pos_before == pos:
            #return
        type = self.full[index + pos_before+1:index + pos + 1]
        self.types.append(type)
        if len(content) <= pos + 1:
            return
        self.__parse_type(index + pos + 1)

class Word(object):
    def __init__(self, line):
        line = line.strip()
        line = line.replace("]", "] ")
        self.full = line
        self.word = ""
        self.chinese = ""
        self.attributes = ""
        self.att = None
    def parse(self):
        self.word = self.__read_word()
        if len(self.word) < 1:
            return
            
        self.chinese = self.__read_comment()
        self.attributes  = self.__read_attributes()
        pos = self.word.find('/')
        if pos > 0:
            att = self.word[pos:]
            self.attributes = att + self.attributes 
            self.word = self.word[:pos]
        pos = self.word.find('[')
        if pos > 0:
            att = self.word[pos:].replace("[", "/").replace("]", "/")
            self.attributes = att + self.attributes 
            self.word = self.word[:pos]
        self.att = WordAttributes(self.attributes)
        self.att.parse()
        self.__parse_type(0)
    def types_str(self):
        if self.att is None:
            return ""
        tp = ""
        for i in self.att.types:
            if tp != "":
                tp +="&"
            tp +=i
        return tp
    def is_phrase(self):
        att = self.attributes
        att = att.strip()
        if len(att) < 1:
            return False
        if att.find("/") >= 0:
            return False
        if att.find("[") >= 0:
            return False
        if att.find("(") >= 0:
            return False
        if att.find("&") >= 0:
            return False
        if len(self.att.types) > 0:
            return False
        return True
    def phrase(self):
        return self.word + " " + self.attributes
    def wordEx(self):
        if self.is_phrase():
            return self.phrase()
        return self.word
    def to_str(self):
        att = self.attributes
        att.replace("\t", "")
        if self.att is not None:
            for type in self.att.types:
                att = att.replace(type, "")
                
        if self.is_phrase():
            att = ""
        return "{}\t{}\t{}\t{}".format(self.wordEx(), self.types_str(), att, self.chinese)
    def __parse_type(self, index):
        content = self.chinese[index:]
        pos = content.find(".")
        if pos < 0:
            return
        pos_before = content.rfind(' ', 0, pos)
        if pos_before < 0:
            return
        #if pos_before == pos:
            #return
        type = self.chinese[index + pos_before+1:index + pos + 1]
        self.att.types.append(type)
        if len(content) <= pos + 1:
            return
        self.__parse_type(index + pos + 1)
        
    def __read_word(self):
        pos = self.full.find(' ')
        if pos < 1:
            return ''
        word = self.full[:pos]
        pos = word.find("、")
        if pos < 1:
            return word
        return word[pos + 1:]
    def __read_comment(self):
        pos = find_first_CHINESE_CHAR(self.full)
        if pos < 1:
            return ''
        return self.full[pos:]
    def __read_attributes(self):
        pos = self.full.find(' ')
        if pos < 1:
            return ''
        chn_pos = find_first_CHINESE_CHAR(self.full)
        if chn_pos < 1:
            return ''
        if chn_pos == pos:
            return ""
        return self.full[pos:chn_pos]